<template>
	<view class="orders-list" :class="[config.style]">
		<!-- #ifdef H5-->
		<Top />
		<!-- #endif -->
		<view class="main-section top">
			<!-- 顶部固定部分 -->
			<view class="top-warp">
				<view class="tabs-block">
					<view class="tabs">
						<ListTabs ref="tabsRef" v-model="tabIndex" :config="config" :tabs="tabs" :height="38" @change="tabChange"></ListTabs>
					</view>
				</view>
			</view>
			<!--数据列表部分-->
			<view class="list-section uni-clearfix">
				<!-- top="xxx"下拉布局往下偏移,防止被悬浮菜单遮住 -->
				<mescroll-uni ref="mescrollRef" @init="mescrollInit" :top="mescrollTop" :down="downOption" @down="downCallback" @up="upCallback">
					<!-- 数据列表 -->
					<view class="list">
						<view class="item"  :id="'swiper_item_' + index" v-for="(item, index) in list" :key="index">
							<view class="base-info">
								<view class="orderno">
									<text>订单号：</text>
									<text>{{item.order_no}}</text>
								</view>
								<view class="status">
									<template>
										<text class="status-btn">{{item.status_str}}</text>
									</template>
								</view>
							</view>
							<view class="content" @click="goToDetailLink(item.products.id)">
								<view class="cover">
									<image mode="widthFix" :src="item.products.cover_400" @error="imageError" class="image"></image>
								</view>
								<view class="info">
									<view class="title text-ellipsis-2">{{item.products.title}}</view>
									<view class="description text-ellipsis">{{item.products.description}}</view>
									<view class="price">
										<text class="title">积分：</text>
										<text>{{item.price}}</text>	
									</view>
									<view class="quantity">
										<text class="title">数量：</text>
										<text>x {{item.goods_num}}</text>	
									</view>
								</view>
							</view>
							
							<view class="action-info">
								<!--待付款-->
								<view class="item" v-if="item.paid == 0 && item.status == 0">
									<view class="action-btn pay-btn">
										<button type="default" size="mini" @click="pay('muu_scoreshop',item.order_no)">立即兑换</button>
									</view>
									<view class="action-btn cannel-btn">
										<button type="default" size="mini" @click="cancel(item.id,index)">取消订单</button>
									</view>
								</view>
								<!--待发货-->
								<view class="item" v-if="item.status == 0">
									
								</view>
								<!--待收货-->
								<view class="item" v-if="item.status == 1">
									<view class="action-btn logistic-btn" v-if="item.products.type == 0">
										<button type="default" size="mini" @click="logistic(item.id,index)">查看物流</button>
									</view>
									<view class="action-btn logistic-btn" v-if="item.products.type == 1">
										<button type="default" size="mini" @click="delive(item.id,index)">发货信息</button>
									</view>
									<view class="action-btn confirm-btn">
										<button type="default" size="mini" @click="confirm(item.id,index)">确认收货</button>
									</view>
								</view>
								<!--待评价-->
								<view class="item" v-if="item.status == 2">
									<view class="action-btn evaluate-btn">
										<button type="default" size="mini" @click="evaluate(item.order_no)">立即评价</button>
									</view>
								</view>
								<!--已完成-->
								<view class="item" v-if="item.status == 3 && item.evaluate.edit == true">
									<view class="action-btn evaluate-btn">
										<button type="default" size="mini" @click="evaluate(item.order_no)">编辑评价</button>
									</view>
								</view>
							</view>
						</view>	
					</view>
				</mescroll-uni>
			</view>
		</view>
		<!--物流查询popup弹窗-->
		<uni-popup ref="logisticDetail" type="center">
			<view class="logistic-popup">
				
				<view class="detail-item">
					<view class="title">
						物流查询
					</view>
					<view class="order-no">
						<text>订单号：</text>
						<text>{{logisticData.OrderCode}}</text>
					</view>
					<view class="shiper-name">
						<text>{{logisticData.ShipperName}}</text>
					</view>
					<view class="logistic-code">
						<text>单号：</text>
						<text>{{logisticData.LogisticCode}}</text>
					</view>
					
					<view class="content">
						<view class="item" v-for="(item,index) in logisticData.Traces" :key="index">
							<text class="time">{{item.AcceptTime}}</text>
							<text class="station">{{item.AcceptStation}}</text>
							<text class="remark">{{item.Remark}}</text>
						</view>
					</view>
				</view>
				<view class="close-icon" @click="closeLogistic()">
					<text class="iconfont">&#xe622;</text>
				</view>
			</view>
			
		</uni-popup>
		
		
		<!--虚拟商品发货信息popup弹窗-->
		<uni-popup ref="deliveDetail" type="center">
			<view class="delive-popup">
				
				<view class="detail-item">
					<view class="title">
						发货数据
					</view>
					
					<view class="content">
						<view class="item">
							{{deliveData.content}}
						</view>
					</view>
				</view>
				<view class="close-icon" @click="closeDelive()">
					<text class="iconfont">&#xe622;</text>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js"
	import ListTabs from "../../common/listTabs" //列表tab
	import Top from '../../common/top.vue'
	
	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			ListTabs,
			Top
		},
		data() {
			return {
				urlParams: {}, //url传递过来的参数
				page: 1, //当前页码
				list: [], //列表数据
				tabs: ['全部','待发货','待收货', '待评价','已完成'],
				tabIndex: 0 ,// tab下标
				status: 'all', // 默认全部订单 all:所有 0：待发货 1：待收货 2：待评价 3：已完成
				logisticData: {}, //实物商品物流信息
				deliveData: '', //虚拟商品发货信息
				mescrollTop: 74,
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption:{
					// page: {
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 搜索无数据 ~', // 提示
						btnText: '去看看'
					}
				},
				swiperHeight: 200, //当前swiper高度
			}
		},
		onLoad(options) {
			//获取url参数
			this.urlParams = options
			console.log(this.urlParams,'urlparams')
			//渲染结束后，判断上一页是否更改tabindex参数
			if(this.urlParams.tab_index){
				this.list = []// 先置空列表,显示加载进度
				this.$nextTick(() => {
					this.$refs.tabsRef.tabClick(Number(this.urlParams.tab_index))
				})
			}
		},
		created() {
			// #ifdef H5
			console.log(this.urlParams.tab_index,'tab_index')
			this.mescrollTop = 148
			// #endif
		},
		computed: {
			...mapState(['config'])
		},
		methods: {
			//上拉加载的回调
			upCallback: function() {
				let that = this
				let keyword = this.tabs[this.tabIndex]
				console.log(keyword)
				let data = this.urlParams
					data.action = 'lists'
					data.page = this.page
					data.status = this.status
				//获取首页数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/orders'
					//#endif
					//#ifdef H5
					url = 'entry/site/orders'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							let list = res.data.data.data
							that.list = that.list.concat(list);
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(list.length);
							that.page = that.page + 1
						}
					}
				})
			},
			//下拉刷新
			downCallback: function() {
				let that = this
				let keyword = this.tabs[this.tabIndex]
				this.page = 1
				
				let data = this.urlParams
					data.action = 'lists'
					data.page = this.page
					data.status = this.status
				//获取首页数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/orders'
					//#endif
					//#ifdef H5
					url = 'entry/site/orders'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							//清空列表
							that.list = []
							that.list = res.data.data.data
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(that.list.length);
							that.page = 2
						}
					}
				})
			},
			
			/**
			 * 进入商品详情
			 * @param {Object} id
			 * @param {Object} type
			 */
			goToDetailLink: function(id){
				let urlParams = 'id='+id
				let page = '../../goods/detail'
				
				let url = page + '?' + urlParams
				console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			},
			
			/**
			 * 取消订单
			 * @param {Object} id
			 */
			cancel: function(id,index){
				let that = this
				uni.showModal({
				    title: '提示',
				    content: '确认取消该订单？',
				    success: function (res) {
				        if (res.confirm) {
				            console.log('用户点击确定');
							let url = ''
								//#ifdef MP-WEIXIN
								url = 'entry/wxapp/orders'
								//#endif
								//#ifdef H5
								url = 'entry/site/orders'
								//#endif
							that.util.request({
								url : url,
								data: {
									action: 'cancel',
									id: id
								},
								method: 'POST',
								header: {
									'content-type': 'application/x-www-form-urlencoded'
								},
								success: function (res) {
									if(res.data.code == 200){
										that.list[index].status = -1
										that.list[index].status_str = '已取消'
									}
									uni.showToast({
										title: res.data.msg,
										icon: 'none',
										duration: 2000
									});
									
								}
							})
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			
			/**
			 * 查询物流
			 * @param {Object} id
			 */
			logistic: function(id,index){
				let that = this
				//清除现有数据
				that.logisticData = {}
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/orders'
					//#endif
					//#ifdef H5
					url = 'entry/site/orders'
					//#endif
				that.util.request({
					url : url,
					data: {
						action: 'logistic',
						id: id
					},
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function (res) {
						if(res.data.code == 200){
							console.log(res.data.data)
							that.logisticData = res.data.data
							//打开弹窗
							that.$refs.logisticDetail.open()
						}
					}
				})
			},
			closeLogistic: function(){
				this.$refs.logisticDetail.close()
			},
			
			/**
			 * 虚拟商品发货信息
			 */
			delive(id,index){
				let that = this
				//清除现有数据
				that.deliveData = ''
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/orders'
					//#endif
					//#ifdef H5
					url = 'entry/site/orders'
					//#endif
				that.util.request({
					url : url,
					data: {
						action: 'delive',
						id: id
					},
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function (res) {
						if(res.data.code == 200){
							console.log(res.data.data)
							that.deliveData = res.data.data
							//打开弹窗
							that.$refs.deliveDetail.open()
						}
					}
				})
			},
			
			closeDelive(){
				this.$refs.deliveDetail.close()
			},
			/**
			 * 确认收货
			 * @param {Object} id
			 */
			confirm: function(id,index){
				let that = this
				uni.showModal({
				    title: '提示',
				    content: '确认收货？',
				    success: function (res) {
				        if (res.confirm) {
				            console.log('用户点击确定');
							let url = ''
								//#ifdef MP-WEIXIN
								url = 'entry/wxapp/orders'
								//#endif
								//#ifdef H5
								url = 'entry/site/orders'
								//#endif
							that.util.request({
								url : url,
								data: {
									action: 'confirm',
									id: id
								},
								method: 'POST',
								header: {
									'content-type': 'application/x-www-form-urlencoded'
								},
								success: function (res) {
									if(res.data.code == 200){
										that.list[index].status = 3
										that.list[index].refund_str = '已收货'
									}
									uni.showToast({
										title: res.data.msg,
										icon: 'none',
										duration: 2000
									});
									
								}
							})
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			
			/**
			 * 发表评价
			 */
			evaluate: function(order_no){
				//console.log(order_no)
				let url = '../../evaluate/edit?' + 'order_no=' + order_no
				uni.navigateTo({
					url: url
				});
			},
			
			/**
			 * 切换TAB
			 * @param {Object} index
			 * all:所有 0：待付款 1：待发货 2：待收货 3：待评价 4:已完成
			 */
			tabChange(index) {
				console.log(this.urlParams)
				this.page = 1
				this.list = []// 先置空列表,显示加载进度
				if(index == 0){
					this.status = 'all'
				}
				if(index == 1){
					this.status = '0'
				}
				if(index == 2){
					this.status = '1'
				}
				if(index == 3){
					this.status = '2'
				}
				if(index == 4){
					this.status = '3'
				}
				console.log(this.status)
				this.mescroll.resetUpScroll() // 再刷新列表数据
			},
			
			//手动切换题目
			changeSwiper(e) {
				console.log(e)
				this.tabIndex = e.detail.current;
				this.tabChange(this.tabIndex)
				//this.questionIndex = this.currentIndex + 1
				//动态设置swiper的高度，使用nextTick延时设置
				this.$nextTick(() => {
					this.setSwiperHeight();
				});
			},
			
			setSwiperHeight(){
				let that = this
				let element = "#swiper_item_" + this.currentIndex
				console.log(element)
				let query = uni.createSelectorQuery().in(this)
				query.select(element).boundingClientRect(res => {
					console.log(res)
					if (res) {
						that.swiperHeight = res.height
					}
				}).exec()
			},
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #f8f8f8;
		min-height: 100%;
		height: auto;
	}
	
	/*顶部固定部分*/
	.top-warp {
		background-color: #ffffff;
		.tabs-block {
			display: flex;
			flex-wrap: wrap;
			margin: 0 20rpx;
			.tabs {
				flex: 1;
			}
		}
	}
	.list {
		/*小图*/
		padding: 0 10rpx;
		display: block;

		.item {
			box-sizing:border-box;
			background-color: #fff;
			margin: 10rpx;
			border-radius: 10rpx;
			box-shadow: 0 0 10rpx rgba(0,0,0,.02);
			overflow: hidden;
			.base-info {
				display: flex;
				font-size: 28rpx;
				border-bottom: 2rpx solid #eee;
				line-height: 64rpx;
				color: #666;
				padding: 0 20rpx;
				.orderno {
					flex: 1;
				}
				.status {
					width: 180rpx;
					text-align: right;
					.status-btn,.refund-btn {
						color: red;
					}
					.status-btn-disable {
						color: #999;
					}
				}
			}
			.content {
				display: flex;
				padding: 20rpx;
				.cover {
					width: 280rpx;
					overflow: hidden;
					.image {
						border-radius: 10rpx;
						width: 100%;
						height: auto;
						display: block;
					}
				}
				.info {
					flex: 1;
					padding: 0 0 0 20rpx;
					.title {
						font-size: 28rpx;
						color: #000;
						font-weight: 600;
						height: 72rpx;
						line-height: 36rpx;
					}
					.description {
						color: #999;
						font-size: 24rpx;
					}
					.price {
						margin-top: 20rpx;
						font-size: 26rpx;
					}
					.quantity {
						font-size: 24rpx;
					}
					.sku {
						font-size: 24rpx;
					}
				}
			}
			.other-info {
				padding: 20rpx;
				text-align: right;
				font-size: 24rpx;
				color: #666;
				.num {
					color: #000;
				}
				.price {
					color: #000;
				}
			}
			.action-info {
				border-top: 2rpx solid #eee;
				color: #666;
				padding: 0 10rpx 0 10rpx;
				.item{
					font-size: 28rpx;
					display: flex;
					flex-direction: row-reverse;
					.action-btn {
						padding: 10rpx;
						
						button {
							display: block;
							color: #fff;
							&::after {
								border: 0;
							}
						}
					}
					.pay-btn {
						
					}
					.cannel-btn {
						button {
							color: #999;
						}
					}
					.logistic-btn {
						button {
							background-color: #f90;
							color: #fff;
						}
					}
					.confirm-btn {
						button {
							
						}
					}
					.refund-btn {
						button {
							color: #999;
						}
					}
					.evaluate-btn {
						button {
							background-color: #f90;
							color: #fff;
						}
					}
				}
			}
		}
	}
	
	.logistic-popup {
		
		.detail-item {
			background-color: #fff;
			border-radius: 10rpx;
			padding: 20rpx;
			color: #666;
			margin: 0 60rpx;
			.title {
				font-size: 28rpx;
				font-weight: 700;
				text-align: center;
				color: #000;
				margin-bottom: 20rpx;
			}
			.order-no {
				font-size: 24rpx;
			}
			.shiper-name {
				font-size: 24rpx;
			}
			.logistic-code {
				font-size: 24rpx;
			}
			.content {
				font-size: 24rpx;
				border-top: 1rpx solid #ddd;
				padding: 10rpx;
				margin-top: 10rpx;
				.item {
					.time {
						
					}
					.station {
						margin-left: 10rpx;
					}
					.remark {
						margin-left: 10rpx;
						color: red;
					}
				}
			}
		}
		.close-icon {
			padding-top: 32rpx;
			text-align: center;
			color: #fff;
			.iconfont {
				font-size: 64rpx;
			}
		}
	}
	
	.delive-popup {
		
		.detail-item {
			min-width: 450rpx;
			background-color: #fff;
			border-radius: 10rpx;
			padding: 20rpx;
			color: #666;
			margin: 0 60rpx;
			.title {
				font-size: 28rpx;
				font-weight: 700;
				text-align: center;
				color: #000;
				margin-bottom: 20rpx;
			}
			.order-no {
				font-size: 24rpx;
			}
			.shiper-name {
				font-size: 24rpx;
			}
			.logistic-code {
				font-size: 24rpx;
			}
			.content {
				font-size: 24rpx;
				border-top: 1rpx solid #ddd;
				padding: 10rpx;
				margin-top: 10rpx;
				.item {
					.time {
						
					}
					.station {
						margin-left: 10rpx;
					}
					.remark {
						margin-left: 10rpx;
						color: red;
					}
				}
			}
		}
		.close-icon {
			padding-top: 32rpx;
			text-align: center;
			color: #fff;
			.iconfont {
				font-size: 64rpx;
			}
		}
	}
	
	.Blue {
		.price {
			color: $Blue;
		}
		.paid-price {
			color: $Blue;
		}
		.pay-btn button{
			background-color: $Blue;
		}
		.confirm-btn button{
			background-color: $Blue;
		}
	}
	.Green {
		.price {
			color: $Green;
		}
		.paid-price {
			color: $Green;
		}
		.pay-btn button{
			background-color: $Green;
		}
		.confirm-btn button{
			background-color: $Green;
		}
	}
	.LightRed {
		.price {
			color: $LightRed;
		}
		.paid-price {
			color: $LightRed;
		}
		.pay-btn button {
			background-color: $LightRed;
		}
		.confirm-btn button{
			background-color: $LightRed;
		}
	}
	.Orange {
		.price {
			color: $Orange;
		}
		.paid-price {
			color: $Orange;
		}
		.pay-btn button {
			background-color: $Orange;
		}
		.confirm-btn button{
			background-color: $Orange;
		}
	}
	.LightPink {
		.price {
			color: $LightPink;
		}
		.paid-price {
			color: $LightPink;
		}
		.pay-btn button {
			background-color: $LightPink;
		}
		.confirm-btn button{
			background-color: $LightPink;
		}
	}
	.Magenta {
		.price {
			color: $Magenta;
		}
		.paid-price {
			color: $Magenta;
		}
		.pay-btn button {
			background-color: $Magenta;
		}
		.confirm-btn button{
			background-color: $Magenta;
		}
	}
</style>
